package PlaneTest;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//游戏主窗口
public class MyGameFrameCS extends Frame {
    Image planeImg = GameUtil.ImageGame("images/FJ.png");
    Image bg = GameUtil.ImageGame("images/BJ.jpg");

    Plane p1 = new Plane(planeImg,100,100,7,25,25);


      @Override
    public void paint(Graphics g) {//g当作一支画笔

        g.drawImage(bg,0,0, Constant.gameWidth, Constant.gameHeight,null);
        p1.drowMyself(g);



    }

    //初始化窗口
    public void launchFrame(){
        this.setTitle("是男人你就坚持20秒");
        setVisible(true);//窗口默认可见
        setSize(Constant.gameWidth,Constant.gameHeight);//设置窗口大小
        setLocation(500,200);//窗口打开的位置
        //增加关闭窗口操作
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//正常退出
            }
        });
        //启动重画窗口的线程
        new PaintThead().start();
        //启动键盘监听
        this.addKeyListener(new keyMonitar());
    }
    //定义一个重画窗口的线程类
    //定义内部内是为了方便直接使用窗口类的相关方法
    class PaintThead extends Thread{
        @Override
        public void run() {
            while (true){
                //内部内可以直接使用外部类的成员
                repaint();
                try {
                    //1s = 1000ms 等于1s画20次
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //内部类设置键盘的监听处理
    class keyMonitar extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
//            System.out.println("按下："+e.getKeyCode());
//            if (e.getKeyCode() == KeyEvent.VK_LEFT){
//                left = true;
//            }
//            if (e.getKeyCode() == KeyEvent.VK_RIGHT){
//                left = true;
//            }
//            if (e.getKeyCode() == KeyEvent.VK_UP){
//                left = true;
//            }
//            if (e.getKeyCode() == KeyEvent.VK_DOWN){
//                left = true;
//            }
            p1.addDirection(e);

        }

        @Override
        public void keyReleased(KeyEvent e) {
//            System.out.println("抬起："+e.getKeyCode());
            p1.minusDirection(e);

        }
    }


    //双缓存解决窗口闪动问题
    private Image offScreenImage = null;
    public void update(Graphics g){
        if (offScreenImage == null)
            //游戏窗口的数据
            offScreenImage = this.createImage(Constant.gameWidth,Constant.gameHeight);
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);


    }



    public static void main(String[] args) {
        MyGameFrameCS gameFrame = new MyGameFrameCS();
        gameFrame.launchFrame();
    }
}
